import time
from Extend.NoSql import SharedData


class LogMethod:
    is_log = True

    @classmethod
    def addLog(cls, error):
        if cls.is_log:
            log = LogModel(error[0], error[1], error[2])
            SharedData.PutLog(log.result)

    # 取出的数据为一条
    @staticmethod
    def getLog():
        return SharedData.GetLog()


class LogModel:
    def __init__(self, type_id: int, log_info: str, log_details=None):
        self.type_id = type_id
        self.log_info = log_info
        self.log_details = log_details
        self.occurrence_time = time.strftime('%F %T', time.localtime(time.time()))
        self.__setLogType()

    def __setLogType(self):
        if self.type_id <= 3:
            self.log_type = "文件分发"
        elif self.type_id <= 5:
            self.log_type = "文件监控"
        elif self.type_id <= 9:
            self.log_type = "数据库操作"
        elif self.type_id <= 13:
            self.log_type = "文件操作"
        elif self.type_id == 14:
            self.log_type = "本地日志生成"
        elif self.type_id <= 18:
            self.log_type = "IMOS模块更新数据分发"
        elif self.type_id <= 22:
            self.log_type = "三维家XML生成"
        elif self.type_id == 69:
            self.log_type = "IMOS订单问题"
        elif self.type_id == 90:
            self.log_type = "错误日志保存问题"
        else:
            self.log_type = ""

    @property
    def result(self):
        return {'type_id': self.type_id, 'log_info': self.log_info, 'log_details': self.log_details,
                'occurrence_time': self.occurrence_time, 'log_type': self.log_type}
